package ac.man.cs.afzal.tcm.terms_sentence_handler;


import ac.man.cs.afzal.tcm.nlp.File_Manager_Impl;
import ac.man.cs.afzal.tcm.nlp.String_Manipulator_Impl;
import ac.man.cs.afzal.tcm.sql_handler.SQL_Handler_Terms_Impl;
import java.io.File;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

/**
 *
 * @author Hammad Afzal
 * @version 1.0
 * Last Modified Date: 16 Jan, 2013
 */
public class Sentence_Splitter_OBM
{

    private int MAX_SENTENCE_LENGTH = 1000;

    private String database_url = "database_url";
    private String username = "username";
    private String password = "password";

    private String ANNOTATED_DOCUMENTS_CORPUS = "ANNOTATED_DOCUMENTS_CORPUS";
    private String ANNOTATED_SENTENCES_CORPUS = "ANNOTATED_SENTENCES_CORPUS";


    private SQL_Handler_Terms_Impl sql_handler = new SQL_Handler_Terms_Impl();
    private File_Manager_Impl fileManager = new File_Manager_Impl();
    private String_Manipulator_Impl strMan = new String_Manipulator_Impl();

    public Sentence_Splitter_OBM()
    {
        Properties props = new Properties();
        try
        {
            props.load(new FileInputStream("Term_Classification.properties"));
            database_url = props.getProperty(database_url);
            username = props.getProperty(username);
            password = props.getProperty(password);

        }
        catch (Exception ex)
        {
            System.out.println("Exception while loading propoerties : " + ex.getCause());
        }

        ANNOTATED_SENTENCES_CORPUS = props.getProperty(ANNOTATED_SENTENCES_CORPUS);
        ANNOTATED_DOCUMENTS_CORPUS = props.getProperty(ANNOTATED_DOCUMENTS_CORPUS);
    }

  
    public void countering_lookup_for_obm_diseases() throws Exception
    {
        String[] tables =
        {
            "Diseases_OBM", "Sentences_Diseases_OBM"
        };

        sql_handler.initialize_parameters(database_url, username, password);
        sql_handler.connect_db();
        sql_handler.reset_tables(tables);


        String input_dir = ANNOTATED_DOCUMENTS_CORPUS;
        String[] list_files = new File(input_dir).list();

        int disease_obm_id = 0;

        String file_contents = "";

        for (String file_in_list: list_files)
        {
            int sentence_count = 0;
            int index = 0;

            String sentence = "";
            

            String document_name = file_in_list.substring(0, file_in_list.length() - 4);
            String[] table_attributes =
            {
                "Disease"
            };
            String[] table_values =
            {
                document_name
            };
            sql_handler.insert_table("Diseases_OBM", table_attributes, table_values);

            File dir_per_disease_obm = new File(ANNOTATED_SENTENCES_CORPUS + "//" + document_name);
            dir_per_disease_obm.mkdir();

            file_contents = fileManager.fileReader(input_dir + "//" + file_in_list).toString();

            while ((index = file_contents.indexOf("<Sentence>", index)) != -1)
            {
                int temp = index + 10;
                index = file_contents.indexOf("</Sentence>", temp);
                sentence = file_contents.substring(temp, index);
                sentence = sentence.replaceAll("'", "");

                if (sentence.length() > MAX_SENTENCE_LENGTH)
                {
                    System.out.println("Sentence too long, Doc ID = " + document_name + "Sentence : " + sentence);
                    sentence = "SENTENCE TOO LONG";
                }

                disease_obm_id = sql_handler.get_disease_obm_id(document_name);
                
                String[] table_attributes_1 =
                {
                    "Disease_ID", "Local_Sen_ID", "Value"
                };
                String[] table_values_1 =
                {
                    Integer.toString(disease_obm_id), Integer.toString(sentence_count), sentence
                };
                sql_handler.insert_table("Sentences_Diseases_OBM", table_attributes_1, table_values_1);

                if (sentence_count < 10)
                {
                    fileManager.fileWriter(sentence,
                            ANNOTATED_SENTENCES_CORPUS + "//" + document_name + "//00"
                            + sentence_count + "__" + document_name + ".txt");
                }
                else if (sentence_count < 100)
                {
                    fileManager.fileWriter(sentence,
                            ANNOTATED_SENTENCES_CORPUS + "//" + document_name + "//0"
                            + sentence_count + "__" + document_name + ".txt");
                }
                else
                {
                    fileManager.fileWriter(sentence,
                            ANNOTATED_SENTENCES_CORPUS + "//" + document_name + "//"
                            + sentence_count + "__" + document_name + ".txt");
                }

                sentence_count++;
                }
            }
        sql_handler.close_db();
    }
}